#! /bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# bacula       This shell script takes care of starting and stopping
#              the bacula Director daemon
#
# chkconfig: 2345 92 99
# description: The Leading Open Source Backup Solution.
#
#  For Bacula release @VERSION@ (@DATE@) -- @DISTNAME@
#

# Source function library
. /etc/rc.d/functions.bacula

RETVAL=0
case "$1" in
    start)
        [ -x ${BACDIRBIN}/bacula-dir ] && { 
            sleep 2
            echo -n "Starting the Director daemon: "
            OPTIONS=''
            if [ "${DIR_USER}" != '' ]; then
               OPTIONS="${OPTIONS} -u ${DIR_USER}"
            fi

            if [ "${DIR_GROUP}" != '' ]; then
               OPTIONS="${OPTIONS} -g ${DIR_GROUP}"
            fi

            ${BACDIRBIN}/bacula-dir $2 ${OPTIONS} -v -c ${BACDIRCFG}/bacula-dir.conf
            RETVAL=$?
            echo Done.
        }
        ;;
    stop)
        [ -x ${BACDIRBIN}/bacula-dir ] && {
            echo -n "Stopping the Director daemon: "
            killproc ${BACDIRBIN}/bacula-dir ${DIR_PORT}
            RETVAL=$?
            echo Done.
        }
        ;;
    restart)
        $0 stop
        sleep 5
        $0 start
        RETVAL=$?
        ;;
    status)
        [ -x ${BACDIRBIN}/bacula-dir ] && status ${BACDIRBIN}/bacula-dir ${DIR_PORT}
        RETVAL=$?
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
        ;;
esac
exit $RETVAL
